package com.example.zhihu.controller;

import com.example.zhihu.pojo.Question;
import com.example.zhihu.service.QuestionService;
import com.example.zhihu.utils.JSONResult;
import com.example.zhihu.utils.PagedResult;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

@Api(value = "问题接口",tags = {"问题接口Controller"})
@RestController
@RequestMapping("/api/question")
public class QuestionController {

    @Autowired
    private QuestionService questionService;

    @ApiOperation("添加问题")
    @PostMapping("/addQuestion")
    public JSONResult addQuestion(@RequestBody Question question) {
        Question res = new Question();
        res = questionService.saveQuestion(question);
        return JSONResult.ok(res);
    }

    @ApiOperation("查询问题")
    @GetMapping("/questionList")
    public JSONResult questionList(@RequestParam(required = false) String name,@RequestParam(required = false)String userId, @RequestParam(required = false) Integer page ) {
        if(page == null) {
            page = 1;
        }
        PagedResult result = questionService.questionList(name, userId,page,8);
        return JSONResult.ok(result);
    }

    @ApiOperation("删除问题")
    @GetMapping("/delQuestion")
    public JSONResult delQuestion(@RequestParam String qid) {
        questionService.delQuestion(qid);
        return JSONResult.ok("删除成功");
    }

    @ApiOperation("编辑问题")
    @PostMapping("/updateQuestion")
    public JSONResult updateQuestion(@RequestBody Question question) {
        questionService.updateQuestion(question);
        return JSONResult.ok("修改成功");
    }
}
